﻿using System.Collections;
using System.Collections.Generic;
using TTGameEngine;
using UnityEngine;

namespace AliveCard {
public class RoomManager : MonoBehaviour {

    public static RoomManager m_instance;

    Dictionary<int, Room> m_roomDict = new Dictionary<int, Room>();

    public GameObject roomRootObj;
    [SerializeField, ReadOnly]
    Room[] m_rooms;

    Room m_curRoom;

    public int CurRoomID {
        get {
            if(m_curRoom) {
                return m_curRoom.m_roomIdx;
            } else {
                return -1;
            };
        }
    }

    public static RoomManager Ins {
        get {
            if(m_instance == null) {
                m_instance = GameObject.FindObjectOfType<RoomManager>();

            }
            return m_instance;
        }
    }


    void Awake() {
        if(m_instance != null && m_instance != this) {
            DebugUtil.LogErrorFormat("[RoomManger] two instance exist! GameObject 1:[{0}] 2:[{1}] ", this.gameObject.name, m_instance.gameObject.name);
        }
        m_instance = this;
        init();
    }

    public void init() {
        Room[] rooms = roomRootObj.GetComponentsInChildren<Room>();
        m_rooms = new Room[rooms.Length];
        foreach(Room room in rooms) {
            if(room.m_roomIdx >= m_rooms.Length) {
                DebugUtil.LogErrorFormat("[RoomMgr] Room的roomIdx参数不对。roomIdx[{0}]", room.m_roomIdx);
            }
            if(m_rooms[room.m_roomIdx] != null) {
                DebugUtil.LogErrorFormat("[RoomMgr] Room的roomIdx参数有重复的。roomIdx[{0}]", room.m_roomIdx);
            }
            m_rooms[room.m_roomIdx] = room;
        }
    }

    public Room GetRoomByIdx(int idx) {
        if (idx < 0 || idx >= m_rooms.Length) return null;

        return m_rooms[idx];
    }

    public void MoveToRoomImm(Room room) {
        if(room.IsLock) {
            string msg = string.Format("Room{0} is Locked.", room.m_roomIdx);
            UIHelper.UIFloatMsg(msg);
            DebugUtil.LogFormat(msg);
            return;
        }
        DebugUtil.LogFormat("MoveToRoom:{0}", room.name);
        //room click
        //room.ClickRoom();
        //hero move
        ACObjectManager.Ins.GetHero().MoveToPositionImm(room.m_standPos.position);
        //camera move
    }

    public void MoveToRoom(Room room) {
        if (room.IsLock)
        {
            string msg = string.Format("Room{0} is Locked.", room.m_roomIdx);
            UIHelper.UIFloatMsg(msg);
            DebugUtil.LogFormat(msg);
            return;
        }
        DebugUtil.LogFormat("MoveToRoom:{0}", room.name);
        //room click
        //room.ClickRoom();
        //hero move
        ACObjectManager.Ins.GetHero().MoveToPosition(room.m_standPos.position);
        //camera move
    }

    public void OnEnterRoom(Room room, ACPuppet p) {
        m_curRoom = room;
        string msg = string.Format("你已进入房间{0}", room.m_roomIdx);
        //UIManager.Ins.ShowUI(UIFormID.UIFloatMsg, new UIFloatMsg.UIFloatMsgData(msg));
        TTEventSystem.Ins.RaiseEvent(GameEventID.GE_EnterRoom);
    }

    public void OnExitRoom(Room room, ACPuppet p) {
        TTEventSystem.Ins.RaiseEvent(GameEventID.GE_ExitRoom);
    }

    public void ActiveAllRoomTrigger(bool b) {
        foreach(Room room in m_rooms) {
            room.ActiveRoomTrigger(b);
        }
    }
}
}
